package com.atguigu.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.oss.service.OssService;
import com.atguigu.oss.utils.ConstantPropertiesUtils;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Service
public class ossServiceImpl implements OssService {
    @Override
    public String uploadFileAvatar(MultipartFile file) {
        String endpoint = ConstantPropertiesUtils.END_POINT;
// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        String buckerName=ConstantPropertiesUtils.BUCKET_NAME;
// 创建OSSClient实例。
        try{
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 上传文件流。
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            String uuid= UUID.randomUUID().toString().replaceAll("-","");
            filename=uuid+filename;
          String datapath=  new DateTime().toString("yyyy/MM/dd");
            filename=datapath+"/"+filename;
          ossClient.putObject(buckerName, filename,inputStream);

            ossClient.shutdown();
            String url="https://"+buckerName+"."+endpoint+"/"+filename;
            return url;
        }catch (Exception e){

        }


// 关闭OSSClient。

        return null;
    }
}
